<?php
namespace app\admin\controller\sys;

use app\BaseController;
use app\common\validate\sys\CommonSysIpConfig as ValidateModel;
use app\common\business\sys\CommonSysIpConfig as serviceModel;
use think\exception\ValidateException;

class CommonSysIpConfig extends BaseController
{

    /*
     * 所有数组
     */
    public function list(){
        $fildes = 'page,word,status';
        $receiveData = getData($this->request->post(),$fildes);
        $where = [];
        if(!empty($receiveData['word'])){
            $where[] = ['ip','like','%'.$receiveData['word'].'%'];
        }
        if(!empty($receiveData['status'])){
            switch ($receiveData['status']){
                case 1:
                    $where[] = ['status','=',0];
                    break;
                case 2:
                    $where[] = ['status','=',1];
                    break;
            }
        }else{
            $where[] = ['status','in',[0,1]];
        }
        $limit_start = ($receiveData['page']-1) * env('page_size');
        $data = [
            'lists' => serviceModel::getList($where,$limit_start,env('page_size')),
            'allpage' => serviceModel::getCount($where)
        ];
        jsonSuccess($data);
    }

    /*
     * 修改和编辑
     */
    public function edit(){
        $fildes = "id,ip,status,remark";
        $receiveData = getData($this->request->post(),$fildes);
        $admin = session('admin_user');
        try {
            if(!empty($receiveData['id'])){
                validate(validateModel::class)->scene('edit')->check($receiveData);
            }else{
                validate(validateModel::class)->scene('add')->check($receiveData);
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            jsonFail($e->getError());
        }
        if(!empty($receiveData['id'])){
            serviceModel::edit($receiveData);
        }else{
            serviceModel::insert($receiveData,$admin['username'].'/'.$admin['mobile']);
        }
        jsonSuccess();
    }

    public function delete(){
        $fildes = "id";
        $receiveData = getData($this->request->post(),$fildes);
        $load = serviceModel::getOne(['id'=>$receiveData['id']]);
        if(empty($load) || (!empty($load) && $load['status'] == -1)){
            jsonFail('数据不存在');
        }
        serviceModel::updateStatus(-1,$load);
        jsonSuccess();
    }

}
